package com.china08.yxyapi.api.manage.tec.teaching.scoreport.updateexam;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.scoreport.Exam;
import com.china08.yxyapi.entity.mg.scoreport.ExamStudent;
import com.china08.yxyapi.entity.mg.scoreport.report.ExamBaseAnalyse;
import com.china08.yxyapi.entity.mg.scoreport.report.ExamQuesAnalyse;
import com.china08.yxyapi.entity.mg.scoreport.report.ExamQuesOverviewAnalyse;
import com.china08.yxyapi.entity.mg.scoreport.report.ExamStuReport;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.repository.mg.scoreport.ExamBaseAnalyseRepository;
import com.china08.yxyapi.repository.mg.scoreport.ExamQuesAnalyseRepsository;
import com.china08.yxyapi.repository.mg.scoreport.ExamQuesOverviewAnalyseRespository;
import com.china08.yxyapi.repository.mg.scoreport.ExamRepository;
import com.china08.yxyapi.repository.mg.scoreport.ExamStuReportRepository;
import com.china08.yxyapi.repository.mg.scoreport.ExamStudentRepository;
import com.china08.yxyapi.util.YxUserPointsUtilV3;

/**
 * 考试成绩列表删除数据
 * 
 */
@CrossOrigin
@RestController
public class DeleteExamController extends YxyApiBaseController {

    @Autowired
    private ExamRepository examRepository;

    @Autowired
    private ExamStudentRepository examStudentRepository;

    @Autowired
    private ExamStuReportRepository examStuReportRepository;

    @Autowired
    private ExamBaseAnalyseRepository examBaseAnalyseRepository;

    @Autowired
    private ExamQuesAnalyseRepsository examQuesAnalyseRepsository;

    @Autowired
    private ExamQuesOverviewAnalyseRespository examQuesOverviewAnalyseRespository;

    @Autowired
    private YxUserPointsUtilV3 yxUserPointsUtilV3;

    @RequestMapping(value = "/api_yw/manage/tec/teaching/scoreport/updateexam/delete", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public void process(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel model)
            throws ParseException {
        model.check();
        String userId = getProfile(request, response).getId();

        Exam exam = examRepository.findOne(model.id);
        List<ExamStudent> examStudentsLst = new ArrayList<ExamStudent>();
        if (null != exam) {
            // 删除考试
            examRepository.delete(exam);
            boolean flagExam = false;
            // 删除学生成绩
            examStudentsLst = examStudentRepository.findByExam(exam);
            for (ExamStudent examStudent : examStudentsLst) {
                examStudentRepository.delete(examStudent);
                flagExam = true;
            }
            // 删除基础分析
            ExamBaseAnalyse analyse = examBaseAnalyseRepository.findByExam(exam);
            if (null != analyse) {
                examBaseAnalyseRepository.delete(analyse);
            }
            // 删除小题分析数据(总体)
            ExamQuesOverviewAnalyse overviewAnalyse = examQuesOverviewAnalyseRespository.findByExam(exam);
            if (null != overviewAnalyse) {
                // 删除小题分析数据(深化)
                List<ExamQuesAnalyse> analysesLst = examQuesAnalyseRepsository.findByExam(exam);
                for (ExamQuesAnalyse examQuesAnalyse : analysesLst) {
                    examQuesAnalyseRepsository.delete(examQuesAnalyse);
                }
                examQuesOverviewAnalyseRespository.delete(overviewAnalyse);
            }
            // 删除学生报告
            List<ExamStuReport> examStuReportsLst = examStuReportRepository.findByExamId(exam.getId());
            boolean flagReports = false;
            for (ExamStuReport examStuReport : examStuReportsLst) {
                examStuReportRepository.delete(examStuReport);
                flagReports = true;
            }
            if (flagReports) {
                // 追加积分
                yxUserPointsUtilV3.delReport(userId);
            }
            if (flagExam) {
                yxUserPointsUtilV3.examDel(userId);
            }
            yxUserPointsUtilV3.delCreate(userId);
        }
    }

    /**
     * 请求参数
     * 
     */
    protected static class ReqModel extends YxyApiReqModel {
        // 考试id
        public String id;

        public void check() {
            Assert.notNull(StringUtils.defaultIfBlank(id, null), "考试编号不能为空");
        }
    }

}
